cap program drop rdrobust_bwselect
program rdrobust_bwselect, rclass

	*version 16.1
	*which rdrobust
	args outcomevar runningvar ifcondition leftright bwselector
	/*noisily*/ rdbwselect2a `outcomevar' `runningvar' `leftright' `ifcondition', ///
	kernel(tri) vce(cluster id) bwselect(`bwselector') 
	local h_l = `e(h_`bwselector'_l)'
	local h_r = `e(h_`bwselector'_r)'
	local b_l = `e(b_`bwselector'_l)'
	local b_r = `e(b_`bwselector'_r)'	
	cap noisily rdrobust `outcomevar' `runningvar' `ifcondition', vce(cluster id) kernel(tri) ///
		h(`e(h_`bwselector'_l)' `e(h_`bwselector'_r)') b(`e(b_`bwselector'_l)' `e(b_`bwselector'_r)') 
	if _rc == 0{
		return local est = `e(tau_bc_`leftright')'
		mat V = e(V_rb_`leftright')
		return local se = sqrt(V[1,1])
		return local tstat = `e(tau_bc_`leftright')' / sqrt(V[1,1])
		return local N = `e(N_`leftright')'

		return local h_l = `e(h_l)'
		return local h_r = `e(h_r)'	
		return local b_l = `e(b_l)'
		return local b_r = `e(b_r)'
		*di "bw_h_l: `bw_h_l'"
		return local success = 1
	}
	else{
	    return local success = 0
	}
end

cap program drop nprobust
program nprobust, rclass
	version 16.1

	args outcomevar runningvar ifcondition
	*preserve
		*keep `ifcondition'
	cap drop evalpoints
	qui gen evalpoints = 0 if _n == 1
	qui sum `runningvar'
	if r(N)>0{
		cap noisily lprobust `outcomevar' `runningvar' `ifcondition', ///
			vce(cluster id) kernel(tri) eval(evalpoints)
		if _rc == 0{
			mat BB = e(Result) 
			return local se = BB[1,8]
			return local est = BB[1,6]
			return local tstat = BB[1,6]/BB[1,8]
			return local h = BB[1,2]
			return local N = `e(N)'
			return local success = 1
		}
		else{
			return local success = 0
		}
	}
	else{
		return local success = 0	
	}
	cap drop evalpoints
end
	